class parkingLot {
  constructor({ isFull, total }) {
    this.total = total;
    this.isFull = isFull;
    this.carList = [];
    this.init();
  }

  init() {
    const { total, isFull } = this;
    if (!this.isNumber(total) || total <= 0) {
      console.error("请输入正确停车场容量");
      return;
    }
    if (isFull) {
      this.carList = Array(total).fill("有人");
      return;
    }
    const occupiedCount = this.getCount(1, total - 1); // 占用数量
    const vacantCount = total - occupiedCount; // 未占用量
    this.carList = [
      ...Array(occupiedCount).fill("有人"),
      ...Array(vacantCount).fill(null),
    ];
  }

  // 停放
  parking(someone) {
    if (someone === null || someone === undefined) {
      console.error("停车对象呢???");
      return;
    }
    const index = this.carList.findIndex((item) => item === null);
    if (index === -1) {
      console.error("没有可用的车位");
      return;
    }
    this.carList.splice(index, 1, someone);
    console.log("停车成功");
  }

  // 取车
  pickUp(id) {
    if (id === null || id === undefined) {
      console.error("取车id不可为空");
      return;
    }
    const index = this.carList.findIndex((item) => item === id);
    if (index === -1) {
      console.error("找不到您的车");
      return;
    }
    this.carList.splice(index, 1, null);
    console.log("取车成功");
  }

  // 是否为数字
  isNumber(value) {
    return !isNaN(parseFloat(value)) && isFinite(value);
  }

  // 某范围内随机生成一个数
  getCount(min, max) {
    return Math.round(Math.random() * (max - min) + min);
  }

  // 获取车位列表
  getCarList() {
    return this.carList;
  }
}
